package chian.mobile.embedding;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.transformer.splitter.TextSplitter;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author cc
 * @version V1.0
 * @date 2025-20 19:39
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class MobileEmbedding {

    @Value("classpath:rule.json")
    private Resource resource;

    private final VectorStore vectorStore;

    @PostConstruct
    public void init() throws Exception{
        // 读取文件的内容
        TextReader textReader = new TextReader(this.resource);
        textReader.getCustomMetadata().put("filename", "citys.txt");
        //拆分
        List<Document> documentList = textReader.get();
        //参数分别是：默认分块大小、最小分块字符数、最小向量化长度（太小的忽略）、最大分块数量、不保留分隔符（\n啥的）
        TextSplitter textSplitter = new TokenTextSplitter (200, 100, 5, 10000, false);
        List<Document> splitDocuments = textSplitter.split(documentList);
        //插入向量库
        this.vectorStore.add(splitDocuments);
        log.info("数据写入向量库成功，数据条数：{}", splitDocuments.size());
    }
}
